package com.wen.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wen.mapper.MerchantMapper;
import com.wen.pojo.Merchant;
import com.wen.pojo.dto.MerchantDto;
import com.wen.pojo.dto.MerchantStatisticsView;
import com.wen.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {

   @Autowired
   private MerchantMapper mapper;

    @Override
    public Page<Merchant> queryByPage(MerchantDto dto, Long pageNum, Long pageSize) {

       Page<Merchant> page = new Page<>(pageNum, pageSize);

        QueryWrapper<Merchant> wrapper = new QueryWrapper<>();
//        if (!StringUtils.isEmpty(dto.getCompanyName())) {
//            wrapper.like("company_name", dto.getCompanyName());
//        }
        if (!StringUtils.isEmpty(dto.getName())){
            wrapper.like("name",dto.getName());
        }

        return mapper.selectPage(page,wrapper);
    }

    @Override
    public List<MerchantStatisticsView> MerchantStatistics(Integer merchantId) {
        return mapper.MerchantStatistics(merchantId);
    }


    @Override
    public List<Merchant> queryAll(MerchantDto dto) {


        QueryWrapper<Merchant> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(dto.getAddress())) {
            wrapper.like("address", dto.getAddress());
        }
        if (!StringUtils.isEmpty(dto.getShopOrName())){
            wrapper.like("shop_name",dto.getShopOrName());
        }

        return mapper.selectList(wrapper);
    }

    @Override
    public Page<Merchant> queryAllTwo(MerchantDto dto) {

        Page<Merchant> page = new Page<Merchant>(dto.getPageNum(), dto.getSize());

        QueryWrapper<Merchant> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(dto.getAddress())) {
            wrapper.like("address", dto.getAddress());
        }
        if (!StringUtils.isEmpty(dto.getShopOrName())){
            wrapper.like("shop_name",dto.getShopOrName());
        }


        return mapper.selectPage(page,wrapper);
    }

    @Override
    public int AddMerchant(Merchant merchant) {
        merchant.setBalance(0);//账户余额
        merchant.setOnOff(0);//门店是否歇业
        merchant.setCreateDate(new Date());//申请时间
        merchant.setCheckTag("S001-1");//审核状态
        merchant.setStatus("C001-1");//是否删除
        int insert = mapper.insert(merchant);
        return insert;
    }
}
